package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.ParallelGatewayConvert;
import cn.com.blueInfo.bpm.template.entity.ParallelGateway;
import cn.com.blueInfo.bpm.template.repository.ParallelGatewayRepository;
import cn.com.blueInfo.bpm.template.request.ParallelGatewayDTO;
import cn.com.blueInfo.bpm.template.response.ParallelGatewayVO;
import cn.com.blueInfo.bpm.template.service.ParallelGatewayService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 并行网关表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ParallelGatewayServiceImpl implements ParallelGatewayService {

    @Autowired
    private ParallelGatewayRepository parallelGatewayRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ParallelGatewayConvert parallelGatewayConvert;

    /**
     * 新增并行网关
     * @param parallelGatewayDTO 并行网关参数
     */
    @Override
    public ResultInfo<Integer> addParallelGateway(ParallelGatewayDTO parallelGatewayDTO) {
        ParallelGateway parallelGateway = parallelGatewayConvert.dtoToEntity(parallelGatewayDTO);
        parallelGateway.setId(snowflakeIdGenerator.nextId());
        Integer addSize = parallelGatewayRepository.createParallelGateway(parallelGateway);
        return ResultInfo.<Integer>builder().data(addSize).message("并行网关成功新增 {} 条").build();
    }

    /**
     * 批量新增并行网关
     * @param parallelGatewayDTOList 并行网关参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddParallelGateway(List<ParallelGatewayDTO> parallelGatewayDTOList) {
        List<ParallelGateway> parallelGatewayList = parallelGatewayConvert.dtoToEntity(parallelGatewayDTOList);
        for (ParallelGateway parallelGateway : parallelGatewayList)
            parallelGateway.setId(snowflakeIdGenerator.nextId());
        Integer addSize = parallelGatewayRepository.batchCreateParallelGateway(parallelGatewayList);
        return ResultInfo.<Integer>builder().data(addSize).message("并行网关成功新增 {} 条").build();
    }

    /**
     * 删除并行网关
     * @param parallelGatewayId 并行网关ID
     */
    @Override
    public ResultInfo<Integer> removeParallelGateway(Long parallelGatewayId) {
        Integer removeSize = parallelGatewayRepository.deleteParallelGateway(parallelGatewayId);
        return ResultInfo.<Integer>builder().data(removeSize).message("并行网关成功删除 {} 条").build();
    }

    /**
     * 批量删除并行网关
     * @param parallelGatewayIdList 并行网关ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveParallelGateway(List<Long> parallelGatewayIdList) {
        Integer removeSize = parallelGatewayRepository.batchDeleteParallelGateway(parallelGatewayIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("并行网关成功删除 {} 条").build();
    }

    /**
     * 更新部分并行网关
     * @param parallelGatewayUpdatePartialDTO 并行网关参数
     */
    @Override
    public ResultInfo<Integer> modifyParallelGatewayPartial(
            ParallelGatewayDTO.UpdatePartialDTO parallelGatewayUpdatePartialDTO) {
        ParallelGateway parallelGateway = parallelGatewayConvert.dtoToEntity(parallelGatewayUpdatePartialDTO);
        Integer modifySize = parallelGatewayRepository.updateParallelGatewayPartial(parallelGateway);
        return ResultInfo.<Integer>builder().data(modifySize).message("并行网关成功更新 {} 条").build();
    }

    /**
     * 更新全部并行网关
     * @param parallelGatewayDTO 并行网关参数
     */
    @Override
    public ResultInfo<Integer> modifyParallelGateway(ParallelGatewayDTO parallelGatewayDTO) {
        ParallelGateway parallelGateway = parallelGatewayConvert.dtoToEntity(parallelGatewayDTO);
        Integer modifySize = parallelGatewayRepository.updateParallelGateway(parallelGateway);
        return ResultInfo.<Integer>builder().data(modifySize).message("并行网关成功更新 {} 条").build();
    }

    /**
     * 查询一条并行网关
     * @param parallelGatewayId 并行网关ID
     */
    @Override
    public ResultInfo<ParallelGatewayVO> queryParallelGatewayById(Long parallelGatewayId) {
        ParallelGateway parallelGateway = parallelGatewayRepository.getParallelGatewayById(parallelGatewayId);
        ParallelGatewayVO parallelGatewayVO = parallelGatewayConvert.entityToVO(parallelGateway);
        return ResultInfo.<ParallelGatewayVO>builder().data(parallelGatewayVO).message("并行网关查询成功").build();
    }

    /**
     * 分页查询并行网关
     * @param parallelGatewayQueryDTO 并行网关参数
     */
    @Override
    public ResultInfo<Page<ParallelGatewayVO>> queryParallelGatewayPage(
            ParallelGatewayDTO.QueryDTO parallelGatewayQueryDTO) {
        Page<ParallelGateway> parallelGatewayPage =
                parallelGatewayRepository.getParallelGatewayPage(parallelGatewayQueryDTO);
        Page<ParallelGatewayVO> resultData = parallelGatewayConvert.convertPage(parallelGatewayPage);
        return ResultInfo.<Page<ParallelGatewayVO>>builder()
                .data(resultData)
                .message("并行网关分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 并行网关 数量
     * @param parallelGatewayQueryDTO 并行网关参数
     */
    @Override
    public ResultInfo<Long> queryParallelGatewayCount(ParallelGatewayDTO.QueryDTO parallelGatewayQueryDTO) {
        Long resultCount = parallelGatewayRepository.getParallelGatewayCount(parallelGatewayQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("并行网关数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 并行网关
     * @param parallelGatewayQueryDTO 并行网关参数
     */
    @Override
    public ResultInfo<List<ParallelGatewayVO>> queryParallelGatewayList(
            ParallelGatewayDTO.QueryDTO parallelGatewayQueryDTO) {
        List<ParallelGateway> parallelGatewayList =
                parallelGatewayRepository.getParallelGatewayList(parallelGatewayQueryDTO);
        List<ParallelGatewayVO> resultData = parallelGatewayConvert.entityListToVoList(parallelGatewayList);
        return ResultInfo.<List<ParallelGatewayVO>>builder()
                .data(resultData)
                .message("并行网关分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
